<?php

function debuga() {
    $arrDados = func_get_args();
    print("<pre>");
    for ($i = 0; $i < count($arrDados); $i++) {
        print("<BR>");
        print_r($arrDados[$i]);
    }
    die();
}

/* require the user as the parameter */
//http://localhost:8080/sample1/webservice1.php?user=1
if (isset($_GET['serial_chip'])) {


    /* soak in the passed variable or set our own */
    $number_of_posts = isset($_GET['num']) ? intval($_GET['num']) : 10; //10 is the default
    $format = strtolower($_GET['format']) == 'json' ? 'json' : 'xml'; //xml is the default
    $serial_mobile = $_GET['serial_mobile']; //no default
    $serial_chip = $_GET['serial_chip']; //no default


    require CAMINHO_SERVIDOR.'conexao.php';
    require CAMINHO_SERVIDOR.'funcoes.php';
    $con = conectar();

    //$query = "SELECT id_usuario FROM tb_usuario WHERE serial_chip='" . $serial_chip . "'";
    $query = "INSERT INTO tb_usuario (nome,serial_mobile, serial_chip, telefone) VALUES('".$_GET['nome']."','".$_GET['serial_mobile']."','".$_GET['serial_chip']."','".$_GET['telefone']."')";
    mysql_query('SET CHARACTER SET utf8');
    $result = mysql_query($query, $con) or die('Errant query:  ' . $query);

   
    //debuga($posts, $query);

    /* output in necessary format */
    if ($format == 'json') {
        header('Content-type: application/json');
        if (empty($posts)) {
            $postVazio = array();
            $postVazio[] = array('id_usuario' => $result);
            echo json_encode(array('posts' => $postVazio));
        } 
    } else {
        header('Content-type: text/xml');
        echo '<posts>';
        foreach ($posts as $index => $post) {
            if (is_array($post)) {
                foreach ($post as $key => $value) {
                    echo '<', $key, '>';
                    if (is_array($value)) {
                        foreach ($value as $tag => $val) {
                            echo '<', $tag, '>', htmlentities($val), '</', $tag, '>';
                        }
                    }
                    echo '</', $key, '>';
                }
            }
        }
        echo '</posts>';
    }

    /* disconnect from the db */
    @mysql_close($con);
}
?>